import 'package:get/get.dart';
import 'package:flutter/material.dart';
import 'package:expansion_tile_card/expansion_tile_card.dart';
import 'package:pass_log/app/core/utils/adapt.dart';
import 'package:pass_log/app/core/utils/logger.dart';
import 'package:pass_log/app/data/services/theme_service.dart';
import 'package:pass_log/app/data/providers/pass_provider.dart';

import '../home_controller.dart';

typedef ObjItem = Map<String, dynamic>;

class HomePassList extends StatelessWidget {
  final List<ObjItem?> passList;
  const HomePassList({super.key, required this.passList});

  static final ctr = Get.find<HomeController>();
  static final plp = Get.find<PassListProvider>();

  @override
  Widget build(BuildContext context) {
    return Column(
      children: passList.map((item) => _buildItem(item)).toList(),
    );
  }

  static ButtonStyle textButtonStyle = TextButton.styleFrom(
    shape: const RoundedRectangleBorder(
      borderRadius: BorderRadius.all(Radius.circular(8.0)),
    ),
  );

  Widget _buildItem(ObjItem? item) {
    var index = passList.indexOf(item);
    return item == null
        ? Container()
        : Padding(
            padding: EdgeInsets.symmetric(horizontal: Adapt.pt(6)),
            child: ExpansionTileCard(
              key: ValueKey(item["id"]),
              elevation: 0,
              leading: CircleAvatar(child: Text((index + 1).toString())),
              title: Text(item["title"], style: TextStyle(fontSize: Adapt.pt(16))),
              expandedTextColor: ThemeService.to.curColorScheme.onPrimaryContainer,
              initialPadding: index == 0
                  ? EdgeInsets.symmetric(vertical: Adapt.pt(6))
                  : EdgeInsets.only(bottom: Adapt.pt(6)),
              finalPadding: index == 0
                  ? EdgeInsets.symmetric(vertical: Adapt.pt(6))
                  : EdgeInsets.only(bottom: Adapt.pt(6)),
              children: [
                Divider(thickness: Adapt.pt(1), height: Adapt.pt(1)),
                Padding(
                  padding: EdgeInsets.symmetric(horizontal: Adapt.pt(12), vertical: Adapt.pt(8)),
                  child: Column(
                    children: [
                      Row(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text("账号：", style: TextStyle(fontSize: Adapt.pt(14))),
                          Expanded(child: Text(item["username"], style: TextStyle(fontSize: Adapt.pt(14)))),
                        ],
                      ),
                      SizedBox(height: Adapt.pt(2)),
                      Row(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text("密码：", style: TextStyle(fontSize: Adapt.pt(14))),
                          Expanded(child: Text(item["password"], style: TextStyle(fontSize: Adapt.pt(14)))),
                        ],
                      ),
                      item["remark"] == "" ? Container() : SizedBox(height: Adapt.pt(2)),
                      item["remark"] == ""
                          ? Container()
                          : Row(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Text("备注：", style: TextStyle(fontSize: Adapt.pt(14))),
                                Expanded(
                                    child: Text(item["remark"], style: TextStyle(fontSize: Adapt.pt(14)))),
                              ],
                            ),
                    ],
                  ),
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: [
                    Expanded(
                      child: TextButton(
                        style: textButtonStyle,
                        child: Column(
                          children: [
                            Icon(Icons.delete, color: Colors.red.shade700),
                            SizedBox(height: Adapt.pt(4)),
                            Text("删除", style: TextStyle(color: Colors.red.shade700)),
                          ],
                        ),
                        onPressed: () {
                          Get.dialog(AlertDialog(
                            title: const Text("系统提示"),
                            content: SizedBox(
                              width: Adapt.pt(300),
                              child: Text(
                                "确定要删除密码【${item["title"]}】吗？",
                                style: TextStyle(fontSize: Adapt.pt(16)),
                              ),
                            ),
                            actions: [
                              TextButton(
                                onPressed: () => Get.back(),
                                child: Text("取消", style: TextStyle(fontSize: Adapt.pt(16))),
                              ),
                              TextButton(
                                onPressed: () {
                                  plp.deletePass(item);
                                  ctr.refreshMainList();
                                  Get.back();
                                },
                                child: Text("确定", style: TextStyle(fontSize: Adapt.pt(16))),
                              ),
                            ],
                          ));
                        },
                      ),
                    ),
                    Expanded(
                      child: TextButton(
                        style: textButtonStyle,
                        child: Column(
                          children: [
                            const Icon(Icons.edit_document),
                            SizedBox(height: Adapt.pt(4)),
                            const Text("编辑"),
                          ],
                        ),
                        onPressed: () async {
                          log(item);
                          await Get.toNamed("/pass?mode=edit", arguments: item);
                          ctr.refreshMainList();
                        },
                      ),
                    ),
                    // Expanded(
                    //   child: TextButton(
                    //     style: textButtonStyle,
                    //     child: Column(
                    //       children: [
                    //         Icon(
                    //           Icons.visibility,
                    //           color: ThemeService.to.curColorScheme.onPrimaryContainer,
                    //         ),
                    //         SizedBox(height: Adapt.pt(4)),
                    //         Text(
                    //           "解密",
                    //           style: TextStyle(color: ThemeService.to.curColorScheme.onPrimaryContainer),
                    //         ),
                    //       ],
                    //     ),
                    //     onPressed: () {},
                    //   ),
                    // ),
                  ],
                ),
              ],
            ),
          );
  }
}
